package com.hnu.highlynewapp.crawler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnu.highlynewapp.entity.ParkEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kLjSumi
 * @Date 2021/1/24
 */
@Component
public class ParkTask {
    @Resource
    private HttpUtil httpUtil;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    @Scheduled(fixedDelay = 100 * 1000)
//    public void doTask() throws IOException {
//        String url = "https://www.hnftp.gov.cn/yshj/yqzt/";
//        String html = httpUtil.doGetHtml(url);
//        List<String> urls = this.getUrl(html);
//        for (String s : urls) {
//            String html1 = httpUtil.doGetHtml(s);
//            parse(html1);
//        }
//        System.out.println("成功");
//    }

    //获取"https://www.hnftp.gov.cn/yshj/yqzt/"下所有园区网址
    public List<String> getUrl(String html) {
        List<String> list = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Elements element = doc.select("div.xin-zdyq-li > ul > li > a");
        for (Element e : element) {
            list.add(e.attr("href"));
        }
        return list;
    }

    private void parse(String html) throws IOException {
        //解析html,获取DOM对象
        Document doc = Jsoup.parse(html);

        ParkEntity parkEntity = new ParkEntity();

        //获取名称
        Elements eName = doc.select("div.yuanqu0409-lm1-r > h1");
        String name = eName.text();
        parkEntity.setParkName(name);

        //获取电话
        Elements ePhone = doc.select("div.yuanqu0409-lm1-r-lm1 > p");
        System.out.println(ePhone);

        String phone = ePhone.text();
//        for (Element spuEle : element) {
//            long spu = Long.parseLong(spuEle.attr("data-spu"));
//
//            //获取sku
//            Elements skuEles = spuEle.select("li.ps-item");
//            for (Element skuEle : skuEles) {
//                long sku = Long.parseLong(skuEle.select("[data-sku]").attr("data-sku"));
//                //根据sku查询商品数据是否已经保存
//                Item item = new Item();
//                item.setSku(sku);
//                List<Item> list = itemService.findAll(item);
//                if (list.size() > 0) {
//                    continue;
//                }
//
//                //设置商品信息
//                item.setSpu(spu);
//
//                String itemUrl = "https://item.jd.com/" + sku + ".html";
//                item.setUrl(itemUrl);
//
//                String picUrl = "https:" + skuEle.select("img[data-sku]").first().attr("data-lazy-img");
//                picUrl = picUrl.replace("/n9/","/n1/");
//                String picName = httpUtil.doGetImage(picUrl);
//                item.setPic(picName);
//
//                String priceJson = httpUtil.doGetHtml("https://p.3.cn/prices/mgets?skuIds=J_" + sku);
//                double price = MAPPER.readTree(priceJson).get(0).get("p").asDouble();
//                item.setPrice(price);
//
//                String itemInfo = httpUtil.doGetHtml(item.getUrl());
//                Document itemDoc = Jsoup.parse(itemInfo);
//                String title = itemDoc.select("div.sku-name").text();
//                item.setTitle(title);
//                item.setCreated(new Date());
//                item.setUpdated(item.getCreated());
//
//                //保存
//                itemService.save(item);
//            }
//        }
    }
}
